package com.juben.svr.demos.web;

<<<<<<< HEAD
import com.juben.svr.domain.ApiResponse;
=======
>>>>>>> 38a1dbe3941ebf5713fbf4c6d3dbf69154c425d3
import com.juben.svr.domain.ResponseMessage;
import com.juben.svr.domain.User;
import com.juben.svr.service.TokenService;
import com.juben.svr.service.UserService;
<<<<<<< HEAD
import com.juben.svr.service.util.CurPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
=======
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
>>>>>>> 38a1dbe3941ebf5713fbf4c6d3dbf69154c425d3

@RestController
@CrossOrigin(origins = "http://localhost:5173",allowedHeaders = "*",allowCredentials = "true")
public class LoginController {
    @Autowired
    private UserService userService;
    private TokenService tokenService;      //不亮，但是删掉报错// 令牌服务，用于生成和处理 JWT 令牌

    @Autowired
    public LoginController(UserService userService, TokenService tokenService) {
        this.userService = userService;// 注入用户服务
        this.tokenService = tokenService;// 注入令牌服务
    }
    // 登录接口
    @PostMapping(value = "/usertlogin")
    public ResponseEntity<ResponseMessage> userlogin(@RequestBody User user){
        System.out.println("前端传来的user:"+user);
<<<<<<< HEAD

=======
>>>>>>> 38a1dbe3941ebf5713fbf4c6d3dbf69154c425d3
        Boolean isLogin = userService.userlogin(user);  // 调用用户服务进行登录验证
        if (isLogin == true){
            // 创建响应消息对象 //// 登录成功，生成 JWT 令牌
            //获取用户信息
            User user1 = userService.getUserByAccount(user.getAccount());
            String token = tokenService.generateTokenForUser(user1); // 生成 JWT 令牌     //确保在你的项目中引入 JWT 的依赖。如果你使用 Maven
            // 获取用户详细信息（如角色、头像等）
            System.out.println("account:"+user.getAccount()+"||"+"phone"+user.getPhone());
            // 获取用户详细信息,变量范围
            User userInfo = null;

            if (user.getAccount() != null) {
                userInfo = userService.getUserByAccount(user.getAccount()); // 获取用户详细信息
            }else {
                userInfo = userService.getUserByPhone(user.getPhone());
            }
            System.out.println("token: " + token);
            System.out.println("userInfo: " + userInfo);
<<<<<<< HEAD
            System.out.println("?????将用户信息添加到缓存？？？？？？？？？？？？");
            CurPool.curUserPool.put(userInfo.getAccount(),userInfo);    // 将用户信息添加到缓存
            System.out.println("用户信息已成功添加到缓存: " + userInfo);
            if (isUserInCache(userInfo.getAccount())) {
                System.out.println("用户信息已成功缓存: " + userInfo);
            } else {
                System.err.println("用户信息未能缓存!");
            }
=======
>>>>>>> 38a1dbe3941ebf5713fbf4c6d3dbf69154c425d3
            ResponseMessage response = new ResponseMessage(200,"登录", token, userInfo);
            return ResponseEntity.ok(response);
        }else {
            // 返回401状态码和空消息
            return ResponseEntity.status(200).body(new ResponseMessage(401,"账号或密码错误", null,null));
        }
    }
<<<<<<< HEAD

    // 注销
    @GetMapping("/loginOut")
    public ResponseEntity<ApiResponse> loginOut(@RequestParam String account) {
        System.out.println("————————————————————————————用户"+account+"注销————————————————————————————————————");
        System.out.println("当前用户池"+CurPool.curUserPool);

        // 确保用户名不为空
        if (account == null || account.isEmpty()) {
            return  ResponseEntity.status(200).body(new ApiResponse(400,"用户名为空",null));
        }

        // 从用户池中移除用户
        User user = CurPool.curUserPool.remove(account); // 直接从用户池中移除并保存 user
        if (user != null) {
            // 如果用户存在，执行清理操作
            CurPool.sessionPool.remove(user.getUID());
            CurPool.webSockets.remove(user.getUID());
            // 使用日志记录操作
            System.out.println("【websocket消息】连接断开，总数为:"+CurPool.webSockets.size());
//            logger.info("用户 {} 已退出登录，WebSocket 连接数为: {}", name, CurPool.webSockets.size());
        } else {
            return ResponseEntity.status(200).body(new ApiResponse(400,"用户不存在",null));
        }

        return ResponseEntity.ok(new ApiResponse(200, "注销成功", null));
    }

    public boolean isUserInCache(String token) {
        return CurPool.curUserPool.containsKey(token);
    }

=======
>>>>>>> 38a1dbe3941ebf5713fbf4c6d3dbf69154c425d3
}
